// @/utils/axios.js
import axios from 'axios'
import { getToken } from './auth'

const service = axios.create({
  baseURL: process.env.VUE_APP_API_BASE,
  timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(config => {
  const token = getToken()
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 响应拦截器
service.interceptors.response.use(
  response => {
    if (response.data.code !== 200) {
      return Promise.reject(response.data)
    }
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      // 处理Token过期
    }
    return Promise.reject(error)
  }
)

export default service
